<?php


namespace App\Services;

use App\Models\Product;

class ProductService
{
    // 热门商品
    public function hotIndex($limit = 10, $is_webapp)
    {
        $products = Product::query()->where('on_sale', true)
            ->orderBy('sold_count', 'desc')
            ->get()
            ->take($limit);

        if ($is_webapp) {
            $products = $this->toWebAppDateFormat($products);
        }

        return $products;
    }

    // 最新商品
    public function newIndex($limit = 10, $is_webapp = false)
    {
        $products = Product::query()->where('on_sale', true)
            ->orderBy('created_at', 'desc')
            ->get()
            ->take($limit);

        if ($is_webapp) {
            $products = $this->toWebAppDateFormat($products);
        }

        return $products;
    }

    // 推荐商品
    public function recommendIndex($limit = 10, $is_webapp = false)
    {
        $products = Product::query()->where('on_sale', true)
            ->inRandomOrder()
            ->get()
            ->take($limit);

        if ($is_webapp) {
            $products = $this->toWebAppDateFormat($products);
        }

        return $products;
    }

    // 猜你你喜欢商品
    public function guessYouLikeIndex($limit = 10, $is_webapp = false)
    {
        $products = Product::query()->where('on_sale', true)
            ->inRandomOrder()
            ->get()
            ->take($limit);

        if ($is_webapp) {
            $products = $this->toWebAppDateFormat($products);
        }

        return $products;
    }

    // 我懒得动前端的样式了, 直接后端改了
    public function toWebAppDateFormat($products)
    {
        return $products->each(
            function ($product) {
                $product->name = $product->title;
                $product->sketch = $product->description;
                $product->picture = $product->image;
                $product->sales = $product->sold_count;

                $product->view = 9999;
                $product->match_point = $product->price;
                $product->cost_price = $product->price > 10 ? $product->price - 10 : $product->price;
                $product->stock = 200;

                unset($product->title);
                unset($product->description);
                unset($product->image);
                unset($product->sold_count);

            }
        );
    }

    public function showProduct(Product $product)
    {
        $product = $product->load(['skus', 'productTags', 'properties', 'brand', 'supplier', 'userFavorite', 'couponCodes']);

        $product->banner = $product->banner
            ? array_map(
                function ($value) {
                    return url("storage/" . $value);
                },
                $product->banner
            )
            : ""; // 图片实际地址

        // 判断当前用户是否收藏本商品
        $product->is_favorite = $product->userFavorite && in_array(
            request()->user()->id,
            $product->userFavorite->pluck('id')->toArray()
        ) ? true : false;
        unset($product->userFavorite);

        $product->stock = $product->skus->pluck('stock')->sum();  // 计算总库存

        $product->view_count = $product->view_count + $product->base_view;  // 查看量 = 虚拟数据 + 真实数据
        unset($product->base_view);

        $product->share_count = $product->share_count + $product->base_share;
        unset($product->base_share);

        $product->recommend_count = $product->recommend_count + $product->base_recommend;
        unset($product->base_recommend);

        // 计算商品销量
        return $product;
    }
}
